package com.compunit.composite.core;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.compunit.composite.core.cx.enumn.EConfigurationTag;
import com.compunit.composite.core.cx.enumn.EPropertiesTag;
import com.compunit.composite.core.cx.enumn.EPropertyTag;
import com.compunit.composite.core.interfaces.IConfigurationExporter;
import com.compunit.composite.core.xml.XMLTag;

/**
 * <code>PropertiesExporter</code> defines implementation
 * of the properties exporter utility unit.
 *
 * @author Yi Wang (Neakor)
 * @version Creation date: 12-05-2009 14:55 EST
 * @version Modified date: 12-05-2009 15:20 EST
 */
public class ConfigurationExporter extends AbstractExporter implements IConfigurationExporter {
	/**
	 * The <code>Map</code> of <code>String</code> fully
	 * qualified component class name to their associated
	 * <code>Properties</code> data.
	 */
	private final Map<String, Properties> cache;
	
	/**
	 * Constructor of <code>PropertiesExporter</code>.
	 */
	public ConfigurationExporter() {
		this.cache = new HashMap<String, Properties>(5);
	}

	@Override
	protected void doExport(final File destination) throws URISyntaxException, IOException {
		final PrintWriter writer = new PrintWriter(destination);
		final String output = this.constructString();
		writer.println(output);
		writer.close();
	}
	
	/**
	 * Construct the XML data string.
	 * @return The <code>String</code> representation
	 * of the cached data in XML format.
	 */
	private String constructString() {
		final XMLTag configTag = new XMLTag(EConfigurationTag.Name.getString());
		for(final String classname : this.cache.keySet()) {
			final Properties properties = this.cache.get(classname);
			if(properties.isEmpty()) continue;
			final XMLTag ptag = new XMLTag(EPropertiesTag.Name.getString());
			ptag.addAttribute(EPropertiesTag.Classname.getString(), classname);
			configTag.addChild(ptag);
			for(final Object key : properties.keySet()) {
				final XMLTag ctag = new XMLTag(EPropertyTag.Name.getString());
				final Object value = properties.get(key);
				ctag.addAttribute(EPropertyTag.Key_PropertyKey.getString(), key.toString());
				ctag.addAttribute(EPropertyTag.Key_PropertyValue.getString(), value.toString());
				ptag.addChild(ctag);
			}
		}
		return configTag.toExternal();
	}

	@Override
	public void put(final String classname, final Properties properties) {
		this.cache.put(classname, properties);
	}

	@Override
	public Properties remove(final String classname) {
		return this.cache.remove(classname);
	}

	@Override
	public boolean contains(final String classname) {
		return this.cache.containsKey(classname);
	}

	@Override
	public Properties get(final String classname) {
		return this.cache.get(classname);
	}

	@Override
	public void cleanup() {
		this.cache.clear();
	}

	@Override
	public boolean isEmpty() {
		return this.cache.isEmpty();
	}
}
